﻿namespace MicroCloud.Extensions
{
    #region "<see cref="IDistributedCache"/> 扩展方法"
    /// <summary>
    /// <see cref="IDistributedCache"/> 扩展方法
    /// </summary>
    public static class DistributedCacheExtensions
    {
        #region "IDistributedCache 扩展方法"
        #region "将对象存入缓存中，使用功能配置"
        /// <summary>
        /// 将对象存入缓存中，使用功能配置
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="function"></param>
        public static void Set(this IDistributedCache cache, string key, object value, IFunction function)
        {
            Check.NotNullOrEmpty(key, nameof(key));
            Check.NotNull(value, nameof(value));
            Check.NotNull(function, nameof(function));

            DistributedCacheEntryOptions options = function.ToCacheOptions();
            if (options == null)
            {
                return;
            }
            cache.Set(key, value, options);
        }
        #endregion
        #region "异步将对象存入缓存中，使用功能配置"
        /// <summary>
        /// 异步将对象存入缓存中，使用功能配置
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="function"></param>
        /// <returns></returns>
        public static Task SetAsync(this IDistributedCache cache, string key, object value, IFunction function)
        {
            Check.NotNullOrEmpty(key, nameof(key));
            Check.NotNull(value, nameof(value));
            Check.NotNull(function, nameof(function));

            DistributedCacheEntryOptions options = function.ToCacheOptions();
            if (options == null)
            {
                return Task.FromResult(0);
            }
            return cache.SetAsync(key, value, options);
        }
        #endregion
        #region "获取指定键的缓存项，不存在则从指定委托获取，并回存到缓存中再返回"
        /// <summary>
        /// 获取指定键的缓存项，不存在则从指定委托获取，并回存到缓存中再返回
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="cache"></param>
        /// <param name="key"></param>
        /// <param name="getFunc"></param>
        /// <param name="function"></param>
        /// <returns></returns>
        public static TResult Get<TResult>(this IDistributedCache cache, string key, Func<TResult> getFunc, IFunction function)
        {
            DistributedCacheEntryOptions options = function.ToCacheOptions();
            if (options == null)
            {
                return getFunc();
            }
            return cache.Get(key, getFunc, options);
        }
        #endregion
        #region "获取指定键的缓存项，不存在则从指定委托获取，并回存到缓存中再返回"
        /// <summary>
        /// 获取指定键的缓存项，不存在则从指定委托获取，并回存到缓存中再返回
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="cache"></param>
        /// <param name="key"></param>
        /// <param name="getAsyncFunc"></param>
        /// <param name="function"></param>
        /// <returns></returns>
        public static Task<TResult> GetAsync<TResult>(this IDistributedCache cache, string key, Func<Task<TResult>> getAsyncFunc, IFunction function)
        {
            DistributedCacheEntryOptions options = function.ToCacheOptions();
            if (options == null)
            {
                return getAsyncFunc();
            }
            return cache.GetAsync(key, getAsyncFunc, options);
        }
        #endregion

        #endregion

    }
    #endregion

}
